//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Globalization;



namespace LitwareHR.Recruiting.Services
{

    /// <summary>
    /// Summary description for FileHelper
    /// </summary>
    public sealed class FileHelper
    {
        private FileHelper()
        {            
        }

        private static string[] mediaFileExtensions = { ".wmv", ".avi", ".mpg", ".mpeg", ".mp4", ".mov", ".movie", ".ogm", ".vfw" };

        public static void CopyFolder(string sourcePath, string destinationPath)
        {
            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }

            if (destinationPath[destinationPath.Length-1] != Path.DirectorySeparatorChar) 
            {
                destinationPath += Path.DirectorySeparatorChar;
            }                 
                       
            foreach(string element in Directory.GetFileSystemEntries(sourcePath))
            {
                // Sub directories
                if( Directory.Exists(element) ) 
                {
                    CopyFolder(element, Path.Combine(destinationPath, Path.GetFileName(element)) );
                }
                // Files in directory
                else 
                {
                    string filePath = Path.Combine(destinationPath, Path.GetFileName(element));
                    File.Copy(element, filePath, true);

                    //remove readonly
                    FileAttributes atts = File.GetAttributes(filePath);                    
                    atts &= ~FileAttributes.ReadOnly;
                    File.SetAttributes(filePath, atts);                    
                }
            }
        }

        public static void MoveFile(string sourceFilePath, string destinationFilePath)
        {
            File.Move(sourceFilePath, destinationFilePath);
        }

        public static void DeleteDirectory(string path)
        {        
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
        }

        public static void DeleteFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

        }
        
        public static bool IsMediaFile(string fileName)
        {
            if (mediaFileExtensions.Contains( Path.GetExtension(fileName)))
            {
                return true;
            }
            return false;
        }

        public static void CreateFileFromByteArray(byte[] value, string filePath)
        {
            using (BinaryWriter binWriter = new BinaryWriter(File.Open(filePath, FileMode.Create)))
            {
                binWriter.Write(value);
            }
        }
    }

}
